package config

import (
	"reflect"
)

type Config struct {
	Server struct {
		Port int
	}
	File struct {
		Root string
	}
}

func (c Config) WalkFields() map[string]any {
	valueOf := reflect.ValueOf(c)
	res := make(map[string]any)
	recusiveFields(valueOf, res)
	return res
}

func recusiveFields(v reflect.Value, fieldsMap map[string]any) {
	t := v.Type()
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		if field.Type.Kind() == reflect.Struct {
			nestMap := make(map[string]any)
			fieldsMap[field.Name] = nestMap
			recusiveFields(v.Field(i), nestMap)
		} else {
			fieldsMap[field.Name] = v.Field(i).Interface()
		}
	}
}
